home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7612 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Memory problems in BC++3.1 for Win.
  5. Date: 24 Feb 1996 10:19:42 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4gmonu$cgl@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Feb 23, 1996 18:33:16 in article <Memory problems in BC++3.1 for Win.>,
  15. '"Michael J.  Cuccia" <z_cucciamj@titan.sfasu.edu>' wrote: 
  16.  
  17.  
  18. >When I try to run a program under Borland's Turbo C++ for  
  19. >Windows 3.1, I get a stack fault when I try to allocate an  
  20. >array of only 100 structures of about 20 characters in  
  21. >length.  I went to the compile|code generation option and  
  22. >made the default memory model LARGE, but that had no  
  23. >effect.  Any suggestions? 
  24. >P.S. I have 8 MB RAM. 
  25. You are using automatic allocation; i.e., on the stack. 
  26. Switch to using dynamic instead.   
  27.  
  28. On machines with rather limited stack space, e.g., the PC, 
  29. the general rule to follow is to use the stack only for trivial 
  30. ephemeral memory requirements an use dynamic for arrays 
  31. and large objects.  Like any rule of thumb, however, use it 
  32. with some discretion -- there's no substitute for intelligent 
  33. thought. 
  34.  
  35. Anyway, the bottom line (now that I'm off the soap box:-)) 
  36.  
  37. Instead of: 
  38.    MyStruct x[100]; 
  39.  
  40. use: 
  41.    MyStruct * z = new MyStruct[100]; 
  42.  
  43. Of course, now that you've allocated the objex from the 
  44. global heap, you must also free the memory when finished 
  45. with it: 
  46.  
  47.    delete [] z; 
  48.  
  49. Of course, you could increase your stack size, but that's not 
  50. a desirable option for "real" programs. Learn to use dynamic 
  51. allocation at an early stage and you're a small jump ahead of 
  52. the others in your class -- er, I mean your school class. 
  53.  
  54. (Looks like I'm in one of my soap-boxy moods today!) 
  55.  
  56. -- 
  57. Pete Grant 
  58. Kalevi, Inc. 
  59. Software Engineering & development
  60.